<?php

define('WEB_ROOT', '/alidata/www/default/');
define('USER_ID', 6);

require_once(WEB_ROOT.'inc/config.php');
require_once(WEB_ROOT.'inc/dt.php');		//数据库模块
require_once(WEB_ROOT.'inc/func.php');		//通用函数

$auth = array('id' => USER_ID, 'name' => dt_query_one("SELECT name FROM user_info WHERE id = ".USER_ID)['name']);

//执行发贴
function do_topic_add($forum_id, $title, $content) {
	global $auth;
	if (dt_query_one("SELECT id FROM forum_topic WHERE title = '$title'")) { echo '重复贴！^_^'; return false; } //标题重复则直接返回

	$forum = dt_query_one("SELECT name, auto_bg_url, auto_intro, topic_limit, city, ext FROM forum WHERE id = $forum_id");
	if (!$forum) return false;

	$rs = dt_query("INSERT INTO forum_topic (forum_id, title, content, user_id, user_name, l_r_user_id, l_r_user_name, l_r_at, top_at, city, orders, c_at) 
		VALUES ('$forum_id', '$title', '$content', ".$auth['id'].", '".$auth['name']."', ".$auth['id'].", '".$auth['name']."', ".time().", ".time().", ".$forum['city'].", 0, ".time().")");
	if (!$rs) return false;

	//更新论坛图标
	$f_bg_url = get_img_url($content);
	$bg_url_cond = (empty($forum['auto_bg_url']) || empty($f_bg_url)) ? '' : ", background_url = '$f_bg_url'";
	//更新论坛简介为帖子标题
	$intro_cond = (empty($forum['auto_intro'])) ? '' : ", intro = '$title'";

	//统计forum数据
	$rs = dt_query("UPDATE forum SET topic_c = topic_c + 1, reply_c = reply_c + 1 $bg_url_cond $intro_cond WHERE id = $forum_id");
	if (!$rs) { echo '更新forum数据失败！'; return false; } 

	if (0 < dt_count('forum', "WHERE id = $forum_id AND today = ".date('Ymd', time()))) {
		$rs = dt_query("UPDATE forum SET today_reply_c = today_reply_c + 1 WHERE id = $forum_id");
	} else {
		$rs = dt_query("UPDATE forum SET today = ".date('Ymd', time()).", today_reply_c = 1, today_up_c = 0 WHERE id = $forum_id");
	}
	if (!$rs) { echo '更新today_reply_c数据失败！'; return false; } 

	return true;
}
